package student.service;

import student.utils.FileUtil;
import student.Main;
import student.dto.StudentDto;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * @author
 * @date 2020/5/9.
 * <p>
 * 学生表：
 * 姓名  年龄  学号
 */
public class StudentService {

    /**
     * 添加 一个学生  在队尾
     */
    public void add() {
        System.out.println("----------请输入 学生姓名---------");

        StudentDto studentDto = new StudentDto();
        Scanner sc = new Scanner(System.in);
        studentDto.setName(sc.next());
        System.out.println("----------请输入 学生年龄---------");
        studentDto.setAge(sc.nextInt());
        System.out.println("----------请输入 学生学号---------");
        studentDto.setNum(sc.next());

        FileUtil fileUtil = new FileUtil();
        fileUtil.queryAll(Main.teacherFilePath);
        System.out.println("----------请输入 需要绑定的老师Id---------");
        studentDto.setTeacherId(sc.nextInt());
        Main.studentDtos.add(studentDto);
        this.delete();
        System.out.println("----------添加成功---------");
        fileUtil.queryAll(Main.studentFilePath);
    }

    /**
     * 在指定位置添加一个学生
     */
    public void addById() {

    }

    /**
     * 删除一个学生
     */
    public void deleteById(Integer id) {
        //过滤掉 需要删除的id
        Main.studentDtos = Main.studentDtos.stream().filter(st -> !Objects.equals(st.getId(), id)).collect(Collectors.toList());
        this.delete();

    }

    private void delete() {
        FileUtil fileUtil = new FileUtil();
        fileUtil.deleteFile(Main.studentFilePath);
        List<String> strings = getStudentStrings();
        try {
            fileUtil.write(Main.studentFilePath, strings);
        } catch (IOException e) {
            System.out.println("删除失败");
            e.printStackTrace();
        }
    }


    private List<String> getStudentStrings() {
        List<String> strings = new ArrayList<>();
        strings.add("ID,姓名,年龄,学号,老师Id");
        for (int i = 0; i < Main.studentDtos.size(); i++) {
            StudentDto studentDto = Main.studentDtos.get(i);
            int index = i + 1;
            strings.add(index + "," + studentDto.getName() + "," + studentDto.getAge() + "," + studentDto.getNum() + "," + studentDto.getTeacherId());
        }
        return strings;

    }


    /**
     * 修改学生姓名
     */
    public void updateName() {
        FileUtil fileUtil = new FileUtil();
        fileUtil.queryAll(Main.studentFilePath);
        System.out.println("----------请输入 需要修改学生的学生Id---------");
        Scanner sc = new Scanner(System.in);
        int id = sc.nextInt();
        StudentDto studentDto = Main.studentDtos.get(id - 1);

        System.out.println("ID,姓名,年龄,学号,老师Id");
        System.out.println(studentDto.getId() + "," + studentDto.getName() + "," + studentDto.getAge() + "," + studentDto.getNum() + "," + studentDto.getTeacherId());
        System.out.println("----------请输入 需要修改后的学生姓名---------");
        String name = sc.next();
        Main.studentDtos.get(id - 1).setName(name);

        this.delete();
        System.out.println("----------修改成功  ---------");
        fileUtil.queryAll(Main.studentFilePath);
    }


    /**
     *  修改学生年龄
     */
    public void updateAge() {
        FileUtil fileUtil = new FileUtil();
        fileUtil.queryAll(Main.studentFilePath);
        System.out.println("----------请输入 需要修改学生的学生Id---------");
        Scanner sc = new Scanner(System.in);
        int id = sc.nextInt();
        StudentDto studentDto = Main.studentDtos.get(id - 1);

        System.out.println("ID,姓名,年龄,学号,老师Id");
        System.out.println(studentDto.getId() + "," + studentDto.getName() + "," + studentDto.getAge() + "," + studentDto.getNum() + "," + studentDto.getTeacherId());
        System.out.println("----------请输入 需要修改后的学生年龄---------");
        int age = sc.nextInt();
        Main.studentDtos.get(id - 1).setAge(age);

        this.delete();
        System.out.println("----------修改成功  ---------");
        fileUtil.queryAll(Main.studentFilePath);
    }
}
